﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Grade.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Grade;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.ViewQuery;

public partial class sysadmin_GradeManager_UserGradeStatistics : PowerCreator.LMS.Web.Base.WebBasePage
{
    GradeManager objGradeManager = new GradeManager();
    ViewQuery objViewQuery = new ViewQuery();
    Course objCourseManager = new Course();

    const string SEARCH_TTEACHER_TGRADE_TUSER = "select DISTINCT TUser.UserID,TUser.UserName from TUser,TGrade where TUser.UserID=TGrade.FKGradeUserID ";
    const string SEARCH_TSKILLTYPE_TSKILL_TGRADE = "select TSkillType.SkillTypeID, SkillTypeName , Sum(GradeScore) as Score from TSkillType left join  TSkill on TSkillType.SkillTypeID=TSkill.FKSkillTypeID" +  
                                                    " left join TGrade on  TGrade.FKGradeSkillID=TSkill.SkillID and TGrade.FKGradeResourceID={0} and FKGradeUserID={1} "+
                                                    "group by TSkillType.SkillTypeName ,SkillTypeID";

    const string SEARCH_TSKILL_TGRADE = "select SkillName,GradeScore from TSkill left join  TGrade on  TGrade.FKGradeSkillID=TSkill.SkillID "+
                                        "where TGrade.FKGradeResourceID={0} and FKGradeUserID={1} and FKSkillTypeId={2}";

    const string SEARCH_TGRADE = "select sum(GradeScore) as avgGradeScore from TGrade where  FKGradeResourceID={0}";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ( Request.QueryString["ResourceID"] == null)
            {
                return;
            }
            
            ViewState["ResourceID"] = Request.QueryString["ResourceID"].ToString();
           
            
            
            InitPage();
            BandPage();
            BinderDownloadType();
            if (dpdlst_gradeUser.Items.Count >0)
            {
                string search = GetSearch(dpdlst_gradeUser.Items[0].Value);
                BinderPager(search);
                BinderPG(search, 1);
            }
           
            if (rptDownload.Items.Count != 0)
            {
                Label lab_SkillTypeID=(Label) rptDownload.Items[0].FindControl("lab_SkillTypeID");
                ViewState["SkillTypeId"] = lab_SkillTypeID.Text;
                string search_GradeParticular = GetSearch_GradeParticular(lab_SkillTypeID.Text,dpdlst_gradeUser.Items[0].Value);
                BinderPager_GradeParticular(search_GradeParticular);
                BinderPG_GradeParticular(search_GradeParticular, 1);
            }
        }


    }
    private void BandPage()
    {
        VOTResource objVOTResource = objCourseManager.LoadResource(int.Parse(ViewState["ResourceID"].ToString()) );
        lab_ResourceName.Text = objVOTResource.ResourceName;
        lab_UserName.Text = objVOTResource.ResourceAuthor;

        
        
    }
    private void BinderDownloadType()
    {
        DataTable dt = new DataTable();

        string search = SEARCH_TTEACHER_TGRADE_TUSER + " and FKGradeResourceID=" + ViewState["ResourceID"].ToString() + "";
        dt = objViewQuery.Query(search);
        if (dt.Rows.Count < 1)
        {
            return;
        }
        dpdlst_gradeUser.DataSource = dt;
        dpdlst_gradeUser.DataTextField = "UserName";
        dpdlst_gradeUser.DataValueField = "UserID";
        
        dpdlst_gradeUser.DataBind();
        dpdlst_gradeUser.Items[0].Selected = true;

        string search_grade = string.Format(SEARCH_TGRADE, ViewState["ResourceID"].ToString());
        DataTable dt_grade = objViewQuery.Query(search_grade, "", LMSPageSize, 1);
        if (dt_grade.Rows.Count > 0)
        {
            lab_AvgScore.Text = Convert.ToString( int.Parse(dt_grade.Rows[0]["avgGradeScore"].ToString()) / dpdlst_gradeUser.Items.Count);
        }

    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerUserGrade.Text =GetLanageValueByKey("lab_AddSkill_ManagerUserGrade", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_ManagerDownload.Text = "新下载资源列表";

        //btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);


        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch(string userid)
    {
        string search = String.Format(SEARCH_TSKILLTYPE_TSKILL_TGRADE, ViewState["ResourceID"].ToString(), userid);
        //if (!string.IsNullOrEmpty(txt_DownLoadTitle.Text))
        //    search = string.Format("ResourceName like '%{0}%'", txt_DownLoadTitle.Text.Trim());
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objViewQuery.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search, int pageIndex)
    {
        DataTable dt = objViewQuery.Query(search, "", LMSPageSize, pageIndex);
        for (int i = 0; i < dt.Rows.Count; i++)
        {

            if (dt.Rows[i]["Score"].ToString().Trim() == "")
            {
                dt.Rows[i]["Score"] = "0";
            }
           
        }
        rptDownload.DataSource = dt;
        rptDownload.DataBind();

        PreAspNetPager(AspNetPager1);
    }

    private string GetSearch_GradeParticular(string FKSkillTypeId,string userid)
    {
        string search = String.Format(SEARCH_TSKILL_TGRADE, ViewState["ResourceID"].ToString(), userid, FKSkillTypeId);
        //if (!string.IsNullOrEmpty(txt_DownLoadTitle.Text))
        //    search = string.Format("ResourceName like '%{0}%'", txt_DownLoadTitle.Text.Trim());
        return search;
    }

    protected void BinderPager_GradeParticular(string search)
    {
        AspNetPager2.RecordCount = objViewQuery.Count(search);
        AspNetPager2.PageSize = LMSPageSize;
    }
    private void BinderPG_GradeParticular(string search, int pageIndex)
    {
        DataTable dt = objViewQuery.Query(search, "", LMSPageSize, pageIndex);
        int index = 1;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["SkillName"] = index.ToString() + "、" + dt.Rows[i]["SkillName"].ToString();
            if (dt.Rows[i]["GradeScore"].ToString().Trim() == "")
            {
                dt.Rows[i]["GradeScore"] = "0";
            }
            index++;
        }
        rptGradeParticular.DataSource = dt;
        rptGradeParticular.DataBind();

        PreAspNetPager(AspNetPager2);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch(dpdlst_gradeUser.Items[0].Value);
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch_GradeParticular(ViewState["SkillTypeId"].ToString(),dpdlst_gradeUser.SelectedValue);
        BinderPG_GradeParticular(search, AspNetPager2.CurrentPageIndex);
    }
    protected void rptDownload_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        

        if (e.CommandName.ToLower() == "see")
        {
            string search_GradeParticular = GetSearch_GradeParticular(e.CommandArgument.ToString(),dpdlst_gradeUser.SelectedValue);
            ViewState["SkillTypeId"] = e.CommandArgument.ToString();
            BinderPager_GradeParticular(search_GradeParticular);
            BinderPG_GradeParticular(search_GradeParticular, 1);
        }


    }
    protected void rptDownload_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }
    protected void rptGradeParticular_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
       

        if (e.CommandName.ToLower() == "see")
        {

        }


    }
    protected void rptGradeParticular_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }


    protected void dpdlst_gradeUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        string search = GetSearch(dpdlst_gradeUser.SelectedValue);
        BinderPager(search);
        BinderPG(search, 1);
        if (rptDownload.Items.Count != 0)
        {
            Label lab_SkillTypeID = (Label)rptDownload.Items[0].FindControl("lab_SkillTypeID");
            string search_GradeParticular = GetSearch_GradeParticular(lab_SkillTypeID.Text, dpdlst_gradeUser.SelectedValue);
            BinderPager_GradeParticular(search_GradeParticular);
            BinderPG_GradeParticular(search_GradeParticular, 1);
        }
    }
}
